﻿using Shop.RemoteModel.GoodsSpec.Model;
using Shop.Repository;

namespace Shop.Collect.lmpl
{
    internal class GoodsSkuStateCollect : IGoodsSkuStateCollect
    {
        private readonly IGoodsSkuStateRepository _Repository;

        public GoodsSkuStateCollect (IGoodsSkuStateRepository repository)
        {
            this._Repository = repository;
        }

        public bool CheckIsNoEnable (long[] skuId)
        {
            return this._Repository.CheckIsNoEnable(skuId);
        }
        public long[] GetNoEnable (long[] skuId)
        {
            return this._Repository.GetNoEnable(skuId);
        }
        public Dictionary<string, bool> GetsSkuState (long goodsId)
        {
            return this._Repository.GetsSkuState(goodsId);
        }

        public bool SetIsEnable (SkuStateSet set)
        {
            var data = this._Repository.Get(a => a.GoodsId == set.GoodsId && a.SpecsKey == set.SpecKey, a => new
            {
                a.Id,
                a.IsEnable
            });
            if (data == null)
            {
                this._Repository.Add(set);
                return true;
            }
            else if (data.IsEnable != set.IsEnable)
            {
                this._Repository.SetIsEnable(data.Id, set.IsEnable);
                return true;
            }
            return false;
        }
    }
}
